package com.adou.Controller;

import com.adou.common.controller.BaseController;
import com.adou.common.model.FlowMain;
import com.adou.common.util.Const;
import com.adou.common.util.StringUtil;
import com.adou.service.FlowMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("flowMain")
@Scope(value = "prototype")
public class FlowMainController extends BaseController {

    @Autowired
    private FlowMainService flowMainService;

    /**
     * 流程列表
     * @param state
     * @param pageNum
     * @param pageSize
     * @param findContent
     * @return
     */
    @GetMapping("pageFlowList")
    private Map<String, Object> pageFlowList(Boolean state, int pageNum, int pageSize, String findContent){
        HashMap<String, Object> map = new HashMap<String, Object>() {
            {
                put("findContent", findContent);
                put("state", state);
            }
        };
        pageNum = pageNum <= 0 ? this.pageNum : pageNum;
        pageSize = pageSize <= 0 ? this.pageSize : pageSize;
        resultMap.putAll(flowMainService.pageList(pageNum, pageSize, map));
        return resultMap;
    }

    /**
     * 添加流程
     * @param flowMain
     * @return
     */
    @PostMapping("insert")
    private  Map<String, Object> insert(FlowMain flowMain){
        if(StringUtil.isBlank(flowMain.getCflowname())){
            error(201,"流程名为空");
            return resultMap;
        }
        if(StringUtil.isBlank(flowMain.getCflowtype())){
            error(202,"流程类型为空");
            return resultMap;
        }
        if(StringUtil.isBlank(flowMain.getCformident())){
            error(203,"表单标识为空");
            return resultMap;
        }
        if(StringUtil.isBlank(flowMain.getAdminId())){
            error(204,"流程管理员为空");
            return resultMap;
        }
        try {
            FlowMain f = new FlowMain();
            f.setCflowname(flowMain.getCflowname());
            List<FlowMain> flowMainList = flowMainService.find(f);
            if(flowMainList.size() > 0){
                error(205, "流程名称重复");
                return resultMap;
            }
            int res = flowMainService.insert(flowMain);
            if (res==0){
                resultMap.put("result",Const.ADD_ERROR_201);
                resultMap.put("msg",Const.SAVE_FAIL);
            }else {
                resultMap.put("msg", Const.SAVE_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.ADD_ERROR_202);
            resultMap.put("msg", Const.SAVE_FAIL);
        }
        return resultMap;
    }

    /**
     * 修改流程
     * @param flowMain
     * @return
     */
    @PostMapping("update")
    private  Map<String, Object> update(FlowMain flowMain){
        if(flowMain.getId()==0){
            error(213,"id为空");
            return resultMap;
        }
        if(StringUtil.isBlank(flowMain.getCflowname())){
            error(214,"流程名为空");
            return resultMap;
        }
        if(StringUtil.isBlank(flowMain.getCflowtype())){
            error(215,"流程类型为空");
            return resultMap;
        }
        if(StringUtil.isBlank(flowMain.getCformident())){
            error(216,"表单标识为空");
            return resultMap;
        }
        try {
            int res = flowMainService.update(flowMain);
            if (res==0){
                resultMap.put("result",Const.UPDATE_ERROR_211);
                resultMap.put("msg",Const.UPDATE_FAIL);
            }
            else if (res==-1){
                error(213,"流程查询错误");
            }
            else if (res==-2){
                error(214,"流程已开始运行，包含运行数据，无法修改表单标识");
            }
            else if (res==-3){
                error(215,"流程已开始运行，包含运行数据，无法修改流程使用人");
            }
            else {
                resultMap.put("msg", Const.UPDATE_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.UPDATE_ERROR_212);
            resultMap.put("msg", Const.UPDATE_FAIL);
        }
        return resultMap;
    }

    /**
     * 根据主键修改状态
     * @param id 流程主键
     * @param bstate 状态
     * @return
     */
    @PostMapping("updateStatusById")
    private Map<String, Object> updateStatusById(Long id,Boolean bstate){
        if (id==0){
            error(201,"id错误");
            return resultMap;
        }
        FlowMain flowMain = new FlowMain();
        flowMain.setId(id);
        flowMain.setBstate(bstate);
        try {
            int res = flowMainService.update(flowMain);
            if (res==0){
                resultMap.put("result",Const.UPDATE_ERROR_211);
                resultMap.put("msg",Const.UPDATE_FAIL);
            }else {
                resultMap.put("msg", Const.UPDATE_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.UPDATE_ERROR_212);
            resultMap.put("msg", Const.UPDATE_FAIL);
        }
        return resultMap;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("delete")
    private Map<String, Object> delete(String ids){
        if(!StringUtil.isInteger(ids.replaceAll(",",""))){
            error(303,"ids格式错误");
            return resultMap;
        }
        try {
            List<FlowMain> list = new ArrayList<>();
            for (String id: ids.split(",")) {
                FlowMain flowMain = new FlowMain();
                flowMain.setId(Long.parseLong(id));
                list.add(flowMain);
            }
            int res = flowMainService.deleteBatch(list);
            if (res==0){
                resultMap.put("result",Const.DEL_ERROR_301);
                resultMap.put("msg",Const.DEL_FAIL);
            }
            else if(res==-1) {
                error(303,"流程运行主表已包含数据，无法删除");
            }
            else {
                resultMap.put("msg", Const.DEL_SUCCEED);
                resultMap.put("data",res);
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DEL_ERROR_302);
            resultMap.put("msg", Const.DEL_FAIL);
        }
        return resultMap;
    }

    /**
     * 流程详情查询
     * @param id
     * @return
     */
    @GetMapping("queryFlowById")
    private Map<String, Object> queryFlowById(Long id){
        try {
            FlowMain o = new FlowMain();
            o.setId(id);
            List<FlowMain> list = flowMainService.find(o);
            if (list.size()==0){
                error(112,"数据为空");
            }else {
                resultMap.put("msg", Const.DATA_SUCCEED);
                resultMap.put("data",list.get(0));
            }
        }catch (Exception e){
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DATA_ERROR_112);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

}
